package com.yanqu.road.server.manager.union;

import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionLockerMgr {

    private static Map<Integer, Object> groupLockerMap = new ConcurrentHashMap<>();
    private static Map<Long, Object> serverLockerMap = new ConcurrentHashMap<>();

    public static Object getGourpLocker(long serverId){
        int groupId = CrossUnionServerGroupMgr.getGroupIdByServerId(serverId);
        if(!groupLockerMap.containsKey(groupId)){
            synchronized (groupLockerMap){
                if(!groupLockerMap.containsKey(groupId)){
                    groupLockerMap.put(groupId, new Object());
                }
            }
        }
        return groupLockerMap.get(groupId);
    }

    public static Object getServerLocker(long serverId){
        if(!serverLockerMap.containsKey(serverId)){
            synchronized (serverLockerMap){
                if(!serverLockerMap.containsKey(serverId)){
                    serverLockerMap.put(serverId, new Object());
                }
            }
        }
        return serverLockerMap.get(serverId);
    }

}
